package com.bridgeintelligent.tag.customer.track;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.bridgeintelligent.tag.customer.mapper.TrackMapper;
import com.bridgeintelligent.tag.customer.model.CustomerModel;
import com.bridgeintelligent.tag.customer.pojo.Filter;
import com.bridgeintelligent.tag.customer.pojo.FilterGroup;
import com.bridgeintelligent.tag.customer.provider.IProviderService;
import com.bridgeintelligent.tag.user.mgmt.pojo.User;
import com.bridgeintelligent.tag.utils.DateHelper;
import com.bridgeintelligent.tag.utils.FieldHandler;
import com.bridgeintelligent.tag.webserver.security.TagSecurityHelper;
import com.wayneleo.quickstart.framework.IDGenerator;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
@Slf4j
public class TrackServiceImpl implements ITrackService {

    private static final String CLASSIFY_CHECK = "01|02|05|06|04";

    private static final String NUM = "NUM";

    private static final String MAX = "MAX";

    private static final String MIN = "MIN";

    private static final String AVG = "AVG";

    private static final String SUM = "SUM";

    /**
     * 客群的客群指标：dimId = custnum标识
     */
    private static final String CUST_NUM = "cust_num";

    private static final String CUST_PRECENT = "cust_precent";

    private static final String SORT_DATE = "dataDate";

    private IProviderService providerService;

    private TrackMapper trackMapper;

    @Override
    public void addTrack(TrackInfo trackInfo) {
        User user = TagSecurityHelper.currentUser();
        trackInfo.setId(IDGenerator.gen())
                .setCreateId(user.getUserId())
                .setCreateTime(DateHelper.currentDateTime(DateHelper.PATTERN_DATE_3))
                .setUpdateTime(DateHelper.currentDateTime(DateHelper.PATTERN_DATE_3))
                .setIsDel(FieldHandler.ZERO)
                .setCustomerSql(this.getSql(trackInfo.getCustomerId()));

        trackMapper.addTrack(trackInfo);
    }

    @Override
    public void updateTrack(TrackInfo trackInfo) {
        //先查询数据库数据
        TrackInfo dbTrack = trackMapper.findById(trackInfo.getId());
        if (dbTrack != null){
            trackInfo.setCreateId(dbTrack.getCreateId())
                    .setCreateTime(dbTrack.getCreateTime());
            trackMapper.delReal(trackInfo.getId());
        }
        trackInfo.setUpdateTime(DateHelper.currentDateTime(DateHelper.PATTERN_DATE_3))
                .setCustomerSql(this.getSql(trackInfo.getCustomerId()))
                .setIsDel(FieldHandler.ZERO);
        trackMapper.addTrack(trackInfo);
    }


    @Override
    public TrackInfo findById(String id) {
        TrackInfo trackInfo = trackMapper.findById(id);
        if (trackInfo != null) {
            //处理标签中文值
            if (!StrUtil.isBlank(trackInfo.getTrackDims())) {
                String[] dims = trackInfo.getTrackDims().split(",");
                Map<String, DimModel> dimMap = new HashMap<>();
                Arrays.asList(dims).forEach(e -> {
                    dimMap.put(e, trackMapper.findClassifyByDimId(e));
                });
                trackInfo.setDimMap(dimMap);
            }
        }
        return trackInfo;
    }

    @Override
    public TrackInfo findByCustomerId(String customerId) {
        TrackInfo trackInfo = trackMapper.findByCustomerId(customerId);
        if (trackInfo != null) {
            //处理标签中文值
            if (!StrUtil.isBlank(trackInfo.getTrackDims())) {
                String[] dims = trackInfo.getTrackDims().split(",");
                Map<String, DimModel> dimMap = new HashMap<>();
                Arrays.asList(dims).forEach(e -> {
                    dimMap.put(e, trackMapper.findClassifyByDimId(e));
                });
                trackInfo.setDimMap(dimMap);
            }
        }
        return trackInfo;
    }

    @Override
    public void delById(String id) {
        trackMapper.delById(id,DateHelper.currentDateTime(DateHelper.PATTERN_DATE_3));
        //删除追踪对应的历史数据
        trackMapper.delData(id);
    }


    @Override
    public TrackPage<TrackInfo> findList (
            String customerName,String timeType,String trackCycle,String trackUptStartTime,String trackUptEndTime,
            String createStartTime,String createEndTime,String trackStatus,Integer pageNum,Integer pageSize
    ){
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 10 : pageSize;
        User user = TagSecurityHelper.currentUser();
        String createId = user.getUserId();
        if (FieldHandler.isUnionAdmin(user.getRoleId())) {
            createId = "";
        }
        String nowTime = DateHelper.currentDateTime(DateHelper.PATTERN_DATE_1);
        List<TrackInfo> list = trackMapper.findList(customerName,timeType,trackCycle,trackUptStartTime,trackUptEndTime,createStartTime,createEndTime,createId,nowTime);
        if (StrUtil.isNotBlank(trackStatus)){
            if (trackStatus.equals("0")){
                list = list.stream().filter(e -> nowTime.compareTo(e.getTrackStartTime()) < 0).collect(Collectors.toList());
            }else if (trackStatus.equals("1")){
                list = list.stream().filter(e -> nowTime.compareTo(e.getTrackStartTime()) >= 0 && nowTime.compareTo(e.getTrackEndTime()) <= 0 ).collect(Collectors.toList());
            }else{
                list = list.stream().filter(e -> nowTime.compareTo(e.getTrackEndTime()) > 0).collect(Collectors.toList());
            }
        }
        if (CollUtil.isEmpty(list)){
            return defaultPage(pageNum, pageSize);
        }
        int count = list.size(); // 总记录数
        // 计算总页数
        int pages = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
        // 起始位置
        int start = pageNum <= 0 ? 0 : (pageNum > pages ? (pages - 1) * pageSize : (pageNum - 1) * pageSize);
        // 终止位置
        int end = pageSize ;

        List<TrackInfo> reqList = list.stream().skip(start).limit(pageSize).collect(Collectors.toList());
        TrackPage<TrackInfo> trackPage = new TrackPage<>();
        trackPage.setContent(reqList)
                .setTotalPages(pages)
                .setTotalElements(Long.parseLong(count+""))
                .setSize(pageSize)
                .setRequestPage(pageNum);
        return trackPage;
    }

    private TrackPage<TrackInfo> defaultPage(Integer pageNum,Integer pageSize){
        TrackPage<TrackInfo> trackPage = new TrackPage<>();
        trackPage.setContent(new ArrayList<>())
                .setTotalPages(0)
                .setTotalElements(0l)
                .setSize(pageSize)
                .setRequestPage(pageNum);
        return trackPage;
    }

    @Override
    public List<CustomerModel> trackCustomerList(String trackId,String indexId, String customerName) {
        User user = TagSecurityHelper.currentUser();
        //如果trackId不为空，需排除该用户已经使用过的客群，但不包括当前trackI所配置的客群
        List<String> list = trackMapper.useCustomerId(trackId, user.getUserId());
        List<CustomerModel> result = trackMapper.trackCustomerList(indexId,customerName,user.getUserId(),list);
        //查詢配置有定制标签的客群集合
        /*List<String> withoutList = trackMapper.withoutCustomerList(indexId, user.getUserId());
        if (!CollUtil.isEmpty(withoutList)){
            result = result.stream().filter(e-> !withoutList.contains(e.getCustomerId())).collect(Collectors.toList());
        }*/
        return result;
    }

    @Override
    public List<List<String>> getRowData(TrackQueryModel queryModel) {
        List<DataModel> trackData = getTrackData(queryModel);
        DimModel dimModel;
        if(CUST_NUM.equals(queryModel.getDimId())){
            dimModel = new DimModel().setDimId(queryModel.getDimId()).setDimName("客群人数").setClassify("05");
        }else{
            dimModel = trackMapper.findClassifyByDimId(queryModel.getDimId());
        }
        if ("01".equals(dimModel.getClassify()) || "04".equals(dimModel.getClassify())){
            return getClassifyOne(trackData,dimModel);
        }else if ("02".equals(dimModel.getClassify()) || "06".equals(dimModel.getClassify())){
            return getClassifyTwoSix(trackData,dimModel);
        }else if ("05".equals(dimModel.getClassify())){
            return getClassifyFive(trackData,dimModel);
        }else{
            return getClassifyThree(trackData,dimModel,queryModel.getCustoms());
        }
    }

    @Override
    public Map<String, String> symbolRelation(String indexId) {
        List<TimeModel> timeList = trackMapper.getTimes(indexId);
        Map<String,String> map = timeList.stream().collect(Collectors.toMap(TimeModel::getSymbol,TimeModel::getEsSymbol));
        return map;
    }

    @Override
    public String getSql(String customerId) {
        FilterGroup filterGroup = providerService.getFilterGroup(customerId);
        return hander(filterGroup);
    }

    @Override
    public TrackPage<SortedModel> getDimData(String trackId, String trackStartTime,String trackEndTime,String sortWord, Boolean sort, String type,Integer pageNum,Integer pageSize) {

        TrackInfo trackInfo = trackMapper.findById(trackId);
        if (trackInfo == null){
            throw new RuntimeException("追踪配置信息不存在！");
        }
        String[] dimIds = trackInfo.getTrackDims().split(",");
        Map<String,Map<String,BigDecimal>> dataMap = new LinkedHashMap<>();
        //获取全量日期集合
        Set<String> dateSet = new HashSet<>();
        //获取相关标签的客数，统计值等
        for (String dimId:dimIds) {
            DimModel dimModel = trackMapper.findClassifyByDimId(dimId);
            List<DataModel> dataModels;
            if (CLASSIFY_CHECK.contains(dimModel.getClassify())){
                dataModels = trackMapper.computTotal(trackId, dimId, trackStartTime, trackEndTime,"");
            }else{
                dataModels = trackMapper.computTotal(trackId, dimId, trackStartTime, trackEndTime,type);
            }
            if (CollUtil.isNotEmpty(dataModels)){
                Map<String, BigDecimal> map = dataModels.stream().collect(Collectors.toMap(DataModel::getDataDate, DataModel::getTotalValue,(v1,v2)->v1));
                dateSet.addAll(map.keySet());
                dataMap.put(dimId,map);
            }else{
                dataMap.put(dimId,new HashMap<>());
            }
        }
        //计算客群维度相关指标:客户数，客数占比等
        if (trackInfo.getIsTrackNum().equals("1")){
            List<TrackData> trackDatas = trackMapper.custTotal(trackId, CUST_NUM,CUST_PRECENT,trackStartTime, trackEndTime);
            if (CollUtil.isNotEmpty(trackDatas)){
                Map<String, List<TrackData>> groupMap = trackDatas.stream().collect(Collectors.groupingBy(TrackData::getDimId));
                if (CollUtil.isNotEmpty(groupMap)){
                    for (String key:groupMap.keySet()) {
                        Map<String, BigDecimal> custMap = groupMap.get(key).stream().collect(Collectors.toMap(TrackData::getDataDate, TrackData::getTargetValue,(v1,v2)->v1));
                        dateSet.addAll(custMap.keySet());
                        dataMap.put(key,custMap);
                    }
                }
            }
        }
        //追加额外指标值
        String appendDims = trackInfo.getTrackDims();
        if (trackInfo.getIsTrackNum().equals("1")){
            appendDims = CUST_NUM+","+CUST_PRECENT+","+appendDims;
        }

        List<String> fields = Arrays.asList(appendDims.split(","));
        // Map<date,Map<field,value>>
        Map<String,Map<String,BigDecimal>> resultMap = new HashMap<>();
        //根据日期组装每行数据
        for (String date: dateSet ) {
            Map<String,BigDecimal> map = new HashMap<>();
            for (String field:fields) {
                map.put(field,Optional.ofNullable(dataMap.get(field).get(date)).orElse(new BigDecimal(0)));
            }
            resultMap.put(date,map);
        }

        if (CollUtil.isEmpty(resultMap)){
            return getDefaultPage(pageNum,pageSize);
        }

        //处理排序数据
        List<SortedModel> sortList = getSortResult( resultMap,sortWord,sort);

        return getPage(sortList,pageNum,pageSize);
    }

    @Override
    public boolean isUsed(String id, String customerId) {

        TrackInfo dbInfo = trackMapper.findByCustomerId(customerId);
        if (dbInfo == null){
            return true;
        }else{
            if (StrUtil.isNotBlank(id) && dbInfo.getId().equals(id)){
                return true;
            }else{
                return false;
            }
        }
    }

    //true 升序，false 降序
    private List<SortedModel> getSortResult(Map<String,Map<String,BigDecimal>> resultMap,String sortWord, Boolean sort){
        List<SortedModel> sortResult = new ArrayList<>();
        if (sortWord.equals(SORT_DATE)){
            List<String> sortDates = new ArrayList<>(resultMap.keySet());
            if (sort){
                sortDates = sortDates.stream().sorted().collect(Collectors.toList());
            }else{
                sortDates = sortDates.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
            }
            for (String date:sortDates) {
                SortedModel model = new SortedModel().setDataDate(date).setFieldMap(resultMap.get(date));
                sortResult.add(model);
            }
        }else{
            Map<BigDecimal,List<String>> tmpMap = new HashMap<>();
            for (String date:resultMap.keySet()) {
                BigDecimal sortValue = resultMap.get(date).get(sortWord);
                List<String> dateList;
                if (tmpMap.get(sortValue) == null){
                    dateList = new ArrayList<>();
                }else{
                    dateList = tmpMap.get(sortValue);
                }
                dateList.add(date);
                tmpMap.put(sortValue,dateList);
            }
            List<BigDecimal> sortValues ;
            if (sort){
                sortValues = tmpMap.keySet().stream().sorted().collect(Collectors.toList());
            }else{
                sortValues = tmpMap.keySet().stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
            }
            List<String> sortDates = new ArrayList<>();
            for (BigDecimal sortValue:sortValues) {
                sortDates.addAll(tmpMap.get(sortValue));
            }
            for (String date: sortDates) {
                SortedModel model = new SortedModel().setDataDate(date).setFieldMap(resultMap.get(date));
                sortResult.add(model);
            }
        }
        return sortResult;
    }

    private TrackPage<SortedModel> getPage(List<SortedModel> sortList,Integer pageNum,Integer pageSize){
        int count = sortList.size(); // 总记录数
        // 计算总页数
        int pages = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
        // 起始位置
        int start = pageNum <= 0 ? 0 : (pageNum > pages ? (pages - 1) * pageSize : (pageNum - 1) * pageSize);
        // 终止位置
        int end = pageSize ;
        List<SortedModel> reqList = sortList.stream().skip(start).limit(pageSize).collect(Collectors.toList());
        TrackPage<SortedModel> pageInfo = new TrackPage<SortedModel>();
        pageInfo.setContent(reqList);
        pageInfo.setTotalPages(pages);
        pageInfo.setTotalElements((Long.parseLong(count+"")));
        pageInfo.setRequestPage(pageNum);
        pageInfo.setSize(pageSize);
       return pageInfo;

    }

    private TrackPage<SortedModel> getDefaultPage(Integer pageNum,Integer pageSize){
        TrackPage<SortedModel> pageInfo = new TrackPage<SortedModel>();
        pageInfo.setContent(new ArrayList<>());
        pageInfo.setTotalPages(0);
        pageInfo.setTotalElements(0l);
        pageInfo.setRequestPage(pageNum);
        pageInfo.setSize(pageSize);
        return pageInfo;

    }

    //处理01,04描述型，标识型(一般不会有标识型)的rowDate
    private List<List<String>> getClassifyOne(List<DataModel> trackData,DimModel dimModel){
        //获取所有的targetKey类型并去重和码值排序
        Set<String> keys = new HashSet<>();
        trackData.forEach(e->{
            keys.addAll(e.getSortMap().keySet());
        });
        //处理第一行title
        List<String> title = new ArrayList<>(keys.size()+2);
        title.add("日期/指标值");
        title.add("总客户数");
        List<String> keyList = keys.stream().collect(Collectors.toList());
        title.addAll(keyList);

        List<List<String>> rowList = new ArrayList<>();
        rowList.add(title);
        for (DataModel model : trackData ) {
            List<String> rowData = new ArrayList<>(keys.size()+2);
            rowData.add(model.getDataDate());
            rowData.add(model.getTotalValue()+"");
            for (String key : keyList ) {
                rowData.add(Optional.ofNullable(model.getSortMap().get(key)+"").orElse(""));
            }
            rowList.add(rowData);
        }
        return rowList;
    }

    //处理02.06的rowDate
    private List<List<String>> getClassifyTwoSix(List<DataModel> trackData,DimModel dimModel){
        //获取所有的targetKey类型并去重和码值排序
        Set<String> keys = new HashSet<>();
        trackData.forEach(e->{
            keys.addAll(e.getSortMap().keySet());
        });
        //获取标签的码值信息
        List<ItemModel> itemList = trackMapper.findItemByDimId(dimModel.getDimId());
        Map<String, String> itemMap = itemList.stream().collect(Collectors.toMap(ItemModel::getItemCode, ItemModel::getItemName));

        //处理第一行title
        List<String> title = new ArrayList<>(keys.size()+2);
        title.add("日期/指标值");
        title.add("总客户数");
        List<String> keyList = keys.stream().collect(Collectors.toList());
        for (String key:keyList) {
            title.add(Optional.ofNullable(itemMap.get(key)).orElse(key));
        }

        List<List<String>> rowList = new ArrayList<>();
        rowList.add(title);
        for (DataModel model : trackData ) {
            List<String> rowData = new ArrayList<>(keys.size()+2);
            rowData.add(model.getDataDate());
            rowData.add(model.getTotalValue()+"");
            for (String key : keyList ) {
                rowData.add(Optional.ofNullable(model.getSortMap().get(key)+"").orElse(""));
            }
            rowList.add(rowData);
        }
        return rowList;
    }

    //处理03 的rowDate
    private List<List<String>> getClassifyThree(List<DataModel> trackData,DimModel dimModel,List<String> customs){
        //如果不是自定义直接取NUM|MAX|MIN|AVG4种值,如果自定义区间则取每个区间的4种值
        int length = customs == null ? 5 : customs.size()*5;
        List<String> title = new ArrayList<>(length+1);
        title.add("日期/指标值");
        if (CollUtil.isEmpty(customs)){
            title.add("客户数");
            title.add("最大值");
            title.add("最小值");
            title.add("平均值");
            title.add("和值");
        }else{
            for (String interval : customs) {
                title.add(interval+"(客户数)");
                title.add(interval+"(最大值)");
                title.add(interval+"(最小值)");
                title.add(interval+"(平均值)");
                title.add(interval+"(和值)");
            }
        }

        List<List<String>> rowList = new ArrayList<>();
        rowList.add(title);
        if (CollUtil.isEmpty(customs)){
            for (DataModel model : trackData ) {
                List<String> rowData = new ArrayList<>(title.size());
                rowData.add(model.getDataDate());
                rowData.add(Optional.ofNullable(model.getSortMap().get(NUM)+"").orElse(""));
                rowData.add(Optional.ofNullable(model.getSortMap().get(MAX)+"").orElse(""));
                rowData.add(Optional.ofNullable(model.getSortMap().get(MIN)+"").orElse(""));
                rowData.add(Optional.ofNullable(model.getSortMap().get(AVG)+"").orElse(""));
                rowData.add(Optional.ofNullable(model.getSortMap().get(SUM)+"").orElse(""));
                rowList.add(rowData);
            }
        }else{
            for (DataModel model : trackData ) {
                List<String> rowData = new ArrayList<>(title.size());
                rowData.add(model.getDataDate());
                for (String interval: customs) {
                    rowData.add(Optional.ofNullable(model.getSortMap().get(interval+"|"+NUM)+"").orElse(""));
                    rowData.add(Optional.ofNullable(model.getSortMap().get(interval+"|"+MAX)+"").orElse(""));
                    rowData.add(Optional.ofNullable(model.getSortMap().get(interval+"|"+MIN)+"").orElse(""));
                    rowData.add(Optional.ofNullable(model.getSortMap().get(interval+"|"+AVG)+"").orElse(""));
                    rowData.add(Optional.ofNullable(model.getSortMap().get(interval+"|"+SUM)+"").orElse(""));
                }
                rowList.add(rowData);
            }
        }
        return rowList;
    }

    //处理05日期型的rowDate
    private List<List<String>> getClassifyFive(List<DataModel> trackData,DimModel dimModel){
        //日期型每个日期只有一个指标数据key = custNum,value = 客户数
        //处理第一行title
        List<String> title = new ArrayList<>(2);
        title.add("日期/指标值");
        title.add("客户数");

        List<List<String>> rowList = new ArrayList<>();
        rowList.add(title);
        for (DataModel model : trackData ) {
            List<String> rowData = new ArrayList<>(2);
            rowData.add(model.getDataDate());
            rowData.add(Optional.ofNullable(model.getSortMap().get(NUM)+"").orElse(""));
            rowList.add(rowData);
        }
        return rowList;
    }

    @Override
    public List<DataModel> getTrackData(TrackQueryModel queryModel) {
        //根据trackId找到配置信息，从而找到标签的类型数据
        TrackInfo trackInfo = trackMapper.findById(queryModel.getTrackId());
        if (trackInfo == null) {
            throw new RuntimeException("未查询到配置信息！");
        }

        DimModel dimModel;
        if(CUST_NUM.equals(queryModel.getDimId())){
            dimModel = new DimModel().setDimId(queryModel.getDimId()).setDimName("客群人数").setClassify("05");
        }else{
            dimModel = trackMapper.findClassifyByDimId(queryModel.getDimId());
        }
        //如果是描述型，标志型，集合型不管是否自定义都走本地查询，日期型标签自定义没有意义，也只走本地
        if (CLASSIFY_CHECK.contains(dimModel.getClassify())){
            List<TrackData> dateList = trackMapper.findDataByTrackId(queryModel.getTrackId(),queryModel.getDimId(),queryModel.getStartTime(),queryModel.getEndTime(),queryModel.getCustoms());
            return getTermResult(dateList);
            //数值型如果customs为空，则走本地
        }else{
            if (CollUtil.isEmpty(queryModel.getCustoms())){
                List<TrackData> dateList = trackMapper.findDataByTrackId(queryModel.getTrackId(),queryModel.getDimId(),queryModel.getStartTime(),queryModel.getEndTime(),null);
                return getNumberResult(dateList);
                //如果为自定义区间范围则需要走hive方式
            }else{
                return null;
            }
        }
    }


    private List<DataModel> getTermResult(List<TrackData> dateList){
        //將dateList根据日期分组
        Map<String, List<TrackData>> dateToMap = dateList.stream().collect(Collectors.groupingBy(TrackData::getDataDate));
        List<DataModel> dataModels = new ArrayList<>();
        for (String date:dateToMap.keySet()) {
            DataModel dataModel = new DataModel();
            BigDecimal count = new BigDecimal("0");
            Map<String,Object> dataMap = new HashMap<>();
            for (TrackData trackData:dateToMap.get(date)) {
                dataMap.put(trackData.getTargetKey(),trackData.getTargetValue());
                count = count.add(trackData.getTargetValue());
            }
            dataModel.setDataDate(date);
            dataModel.setTotalValue(count);
            dataModel.setSortMap(dataMap);
            dataModels.add(dataModel);
        }
        List<DataModel> sortList = dataModels.stream().sorted(Comparator.comparing(DataModel::getDataDate)).collect(Collectors.toList());
        return sortList;
    }

    private List<DataModel> getNumberResult(List<TrackData> dateList){
        //將dateList根据日期分组
        Map<String, List<TrackData>> dateToMap = dateList.stream().collect(Collectors.groupingBy(TrackData::getDataDate));
        List<DataModel> dataModels = new ArrayList<>();
        for (String date:dateToMap.keySet()) {
            DataModel dataModel = new DataModel();
            //如果不是数值型，直接取key-value值;
            Map<String,Object> dataMap = new HashMap<>();
            for (TrackData trackData:dateToMap.get(date)) {
                dataMap.put(trackData.getTargetKey(),trackData.getTargetValue());
            }
            dataModel.setDataDate(date);
            dataModel.setSortMap(dataMap);
            dataModels.add(dataModel);
        }
        List<DataModel> sortList = dataModels.stream().sorted(Comparator.comparing(DataModel::getDataDate)).collect(Collectors.toList());
        return sortList;

    }




    public String hander(FilterGroup filterGroup){
        StringBuffer buffer = new StringBuffer();
        if (filterGroup != null) {
            if (!CollectionUtils.isEmpty(filterGroup.getFilters())) {
                for (int i = 0; i < filterGroup.getFilters().size(); i++) {
                    if (i == (filterGroup.getFilters().size() - 1)) {
                        buffer.append(" [ ");
                        buffer.append(getValue(filterGroup.getFilters().get(i)));
                        buffer.append(" ] ");
                    } else {
                        buffer.append(" [ ");
                        buffer.append(getValue(filterGroup.getFilters().get(i)));
                        buffer.append(" ] ");
                        buffer.append(filterGroup.getSymbol());
                    }
                }
            }
            if (!CollectionUtils.isEmpty(filterGroup.getChildren())) {
                if (!org.springframework.util.CollectionUtils.isEmpty(filterGroup.getFilters())){
                    buffer.append(filterGroup.getSymbol());
                }
                for (int i = 0; i < filterGroup.getChildren().size(); i++) {
                    if (i == filterGroup.getChildren().size()-1){
                        buffer.append(" [ ");
                        buffer.append(hander(filterGroup.getChildren().get(i)));
                        buffer.append(" ] ");
                    }else{
                        buffer.append(" [ ");
                        buffer.append(hander(filterGroup.getChildren().get(i)));
                        buffer.append(" ] ");
                        buffer.append(filterGroup.getSymbol());
                    }

                }
            }
        }
        return buffer.toString();
    }

    public String getValue(Filter filter){

        StringBuffer buffer = new StringBuffer();
        List<String> valueList  = JSON.parseArray(filter.getValueListStr(), String.class);
        String classify = filter.getClassify();//标签类型
        String type = filter.getType();

        DimModel dimModel = trackMapper.findClassifyByDimId(filter.getDimId());
        String largeType = dimModel.getDimLargeType();
        if ("03".equals(classify)){ //如果是数值类
            if (!"termNum".equals(type) && !"noTermNum".equals(type)){
                if ("02".equals(largeType)){
                    //如果是定制标签需要将标签ID转成字符串
                    buffer.append( " cast ( ").append(filter.getDimId()).append(" as string )");
                }else{
                    buffer.append(filter.getDimId());
                }
            }else{
                buffer.append(filter.getDimId());
            }
        }else{
            if ("02".equals(largeType)){
                if (!"term".equals(type) && !"noTerm".equals(type)){
                    //如果是定制标签需要将标签ID转成字符串
                    buffer.append( " cast ( ").append(filter.getDimId()).append(" as string )");
                }else{
                    buffer.append(filter.getDimId());
                }
            }else{
                buffer.append(filter.getDimId());
            }
        }
        buffer.append(getSymbol(filter.getDataDate(),filter.getIndexId(),filter.isTimeType()));

        if("01".equals(classify)){ //描述类型 keyword
            if ("contain_vague".equals(type)){ //包含
                buffer.append(" like '%").append(valueList.get(0)).append("%' ");
            }else if ("not_contain_vague".equals(type)){ //不包含
                buffer.append(" not like '%").append(valueList.get(0)).append("%' ");
            }else if ("term".equals(type)){ //存在
                buffer.append(" is not null ");
            }else if ("noTerm".equals(type)){ //不存在
                buffer.append(" is null ");
            }
        }else if ("02".equals(classify)){ //标志类 keyword
            if ("equal".equals(type)){ // 等于
                buffer.append(" = ").append("'").append(valueList.get(0)).append("' ");
            }else if ("no_equal".equals(type)){ // 不等于
                buffer.append(" != ").append("'").append(valueList.get(0)).append("' ");
            }else if ("contain".equals(type)){ //包含
                buffer.append(" in (").append(value(valueList)).append(") ");
            }else if ("not_contain".equals(type)){ //不包含
                buffer.append(" not in (").append(value(valueList)).append(") ");
            }else if ("term".equals(type)){ //存在
                buffer.append(" is not null ");
            }else if ("noTerm".equals(type)){ //不存在
                buffer.append(" is null ");
            }
        }else if ("03".equals(classify)){ //数值类 double
            if ("greater".equals(type)){ // 大于
                buffer.append(" > ").append(valueList.get(0)).append(" ");
            }else if ("less".equals(type)){ // 小于
                buffer.append(" < ").append(valueList.get(0)).append(" ");
            }else if ("greater_equal".equals(type)){ //大于等于
                buffer.append(" >= ").append(valueList.get(0)).append(" ");
            }else if ("less_equal".equals(type)){ // 小于等于
                buffer.append(" <= ").append(valueList.get(0)).append(" ");
            }else if ("equal".equals(type)){ // 等于
                buffer.append(" = ").append(valueList.get(0)).append(" ");
            }else if ("no_equal".equals(type)){ // 不等于
                buffer.append(" != ").append(valueList.get(0)).append(" ").append(" or ")
                        .append(filter.getDimId()).append(getSymbol(filter.getDataDate(),filter.getIndexId(),filter.isTimeType())+" is null");
            }else if ("among".equals(type)){ //在。。。之中
                buffer.append(" between ").append(valueList.get(0)).append(" and ").append(valueList.get(1)).append(" ");
            }else if ("not_among".equals(type)){ //不在。。。之中
                buffer.append(" not between ").append(valueList.get(0)).append(" and ").append(valueList.get(1)).append(" or ")
                        .append(filter.getDimId()).append(getSymbol(filter.getDataDate(),filter.getIndexId(),filter.isTimeType())+" is null");
            }else if ("termNum".equals(type)){ // 存在
                buffer.append(" is not null "); //不存在
            }else if ("noTermNum".equals(type)){
                buffer.append(" is null ");
            }
        }else if ("05".equals(classify)){ //日期类 date
            if ("greater".equals(type)){ // 大于
                buffer.append(" > '").append(valueList.get(0)).append("' ");
            }else if ("less".equals(type)){ //小于
                buffer.append(" < '").append(valueList.get(0)).append("' ");
            }else if ("greater_equal".equals(type)){ // 大于等于
                buffer.append(" >= '").append(valueList.get(0)).append("' ");
            }else if ("less_equal".equals(type)){ //小于等于
                buffer.append(" <= '").append(valueList.get(0)).append("' ");
            }else if ("equal".equals(type)){ //等于
                buffer.append(" = ").append("'").append(valueList.get(0)).append("' ");
            }else if ("no_equal".equals(type)){ //不等于
                buffer.append(" != ").append("'").append(valueList.get(0)).append("' ");
            }else if ("among".equals(type)){ // 在。。。之中
                buffer.append(" between '").append(valueList.get(0)).append("' and '").append(valueList.get(1)).append("' ");
            }else if ("not_among".equals(type)){ // 不在。。。之中
                buffer.append(" not between '").append(valueList.get(0)).append("' and '").append(valueList.get(1)).append("' ");
            }else if ("term".equals(type)){ // 存在
                buffer.append(" is not null "); //不存在
            }else if ("noTerm".equals(type)){
                buffer.append(" is null ");
            }
        }else if ("06".equals(classify)){ // 集合类 keyword
            if ("contain_everything".equals(type)){
                int i = 0;
                String name = buffer.toString();
                for (String value:valueList){
                    if(i==0){
                        i++;
                        buffer.append(" like '%").append(value).append("%' ");
                        continue;
                    }
                    buffer.append(" or ").append(name).append(" like '%").append(value).append("%' ");
                }
            }
        }
        return buffer.toString();
    }

    private String getSymbol(String dataDate,String indexId,boolean timeType){
        //如果是动态
        if (!timeType){
            if (FieldHandler.ZERO.equals(dataDate) || (FieldHandler.pattern.matcher(dataDate)).matches() && dataDate.length() == FieldHandler.LENGTH8){
                return "_${currentMonth}";
            }else{
                Map<String, String> relation = this.symbolRelation(indexId);
                String symbol = Optional.ofNullable(relation.get(dataDate)).orElse(dataDate);
                return "_${"+symbol+"}";
            }
        }else{
            return "_${"+dataDate+"}";
        }
    }

    private String value(List<String> valus){
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < valus.size(); i++) {
            if (i==valus.size()-1){
                buffer.append("'").append(valus.get(i)).append("'");
            }else{
                buffer.append("'").append(valus.get(i)).append("',");
            }
        }
        return buffer.toString();
    }

}
